package com.dongjiantong.biz.serviceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongjiantong.biz.mapper.DjtFarmReportsFormsMapper;
import com.dongjiantong.biz.model.DjtFarmReportsForms;
import com.dongjiantong.biz.service.DjtFarmReportsFormsService;
import com.dongjiantong.mybatis.ServiceMybatis;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ThinkPad on 2017/11/20.
 */
@Slf4j
@Service(version = "1.0.0")
public class DjtFarmReportsFormsServiceImpl extends ServiceMybatis<DjtFarmReportsForms> implements DjtFarmReportsFormsService {

    @Resource
    private DjtFarmReportsFormsMapper djtFarmReportsFormsMapper;


    /**
     *  根据官方兽id和时间查询产地检疫报表
     *
     * @param pageNum   页码
     * @param pageSize  一页几条
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param vetUserId 官方兽医Id
     * @return
     */
    @Override
    public PageInfo<DjtFarmReportsForms> selectByVetUserIdAndReportTime(int pageNum, int pageSize, Date startTime, Date endTime, Long vetUserId) {
        Example example = new Example(DjtFarmReportsForms.class);
        example.createCriteria().andBetween("report_date", startTime, endTime).andEqualTo("vet_user_id", vetUserId);
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<DjtFarmReportsForms>(djtFarmReportsFormsMapper.selectByExample(example));
    }


    /**
     * 根据动物种类分组  查询合计 ①申报数量（动物数量）②合格数③不合格数
     *
     * @param start 从日期
     * @param end   到日期
     * @param area  区域
     * @return List<Object>
     */
    @Override
    public List<Map<String, Object>> findSumGroupByBreedType(Date start, Date end, String area) {
        Map map = new HashMap();
        map.put("start", start);
        map.put("end", end);
        map.put("area", area);
        return djtFarmReportsFormsMapper.findSumGroupByBreedType(map);
    }

    /**
     * 查询合计--申报数、合格数、不合格数
     *
     * @param start     从日期
     * @param end       到日期
     * @param breedType 产品种类
     * @return List<Object>
     */
    @Override
    public List<Map<String, Object>> findSum(Date start, Date end, Integer breedType, String area) {

        Map map = new HashMap();
        map.put("start", start);
        map.put("end", end);
        map.put("area", area);
        map.put("animal_type", breedType);
        return djtFarmReportsFormsMapper.findSumByBreedTypeAndArea(map);
    }

    /**
     * 查询合计--根据申报的去向
     *
     * @param start     从日期
     * @param end       到日期
     * @param breedType 产品种类
     * @return List<Object>
     */
    @Override
    public List<Map<String, Integer>> findSumByIsToLocal(Date start, Date end, Integer breedType, String area) {

        Map map = new HashMap();
        map.put("start", start);
        map.put("end", end);
        map.put("area", area);
        map.put("animal_type", breedType);
        return djtFarmReportsFormsMapper.findSumByIsToLocal(map);

    }

    /**
     * 查询合计--本地屠宰去向
     *
     * @param start     从日期
     * @param end       到日期
     * @param breedType 产品种类
     * @return List<Object>
     */
    @Override
    public List<Map<String, Object>> findSumGroupByButcherFactory(Date start, Date end, Integer breedType, String area) {
        Map map = new HashMap();
        map.put("start", start);
        map.put("end", end);
        map.put("area", area);
        map.put("animal_type", breedType);
        map.put("isToLocal", 1);
        return djtFarmReportsFormsMapper.findSumGroupByButcherFactory(map);
    }

    /**
     * 查询合计--区域来源分布
     *
     * @param start     从日期
     * @param end       到日期
     * @param breedType 产品种类
     * @return List<Object>
     */
    @Override
    public List<Map<String, Object>> findSumGroupByBreedFactoryTown(Date start, Date end, Integer breedType, String area) {
        Map map = new HashMap();
        map.put("start", start);
        map.put("end", end);
        map.put("area", area);
        map.put("animal_type", breedType);


        return djtFarmReportsFormsMapper.findSumGroupByBreedFactoryTown(map);
    }


    /**
     * 分页查询
     *
     * @param pageNo   页码
     * @param pageSize 每页多少条
     * @param start    从日期
     * @param end      到日期
     * @return Paging<BreedReportsForm>
     */
    @Override
    public PageInfo<DjtFarmReportsForms> pagingBreedReportsForm(Integer pageNo, Integer pageSize, Date start, Date end, Integer breedType, String area) {

        Map map = new HashMap();
        map.put("start", start);
        map.put("end", end);
        map.put("area", area);
        map.put("animal_type", breedType);
        PageHelper.startPage(pageNo, pageSize);
        return new PageInfo<DjtFarmReportsForms>(djtFarmReportsFormsMapper.findByArea(map));
    }


    @Override
    public List<Map> cdjy(Map criteria) {
        return null;
    }

    /**
     * 统计本地产地检疫数量
     * @param
     * @return
     */
    @Override
    public Integer local(Map criteria) {


        return djtFarmReportsFormsMapper.countByIsToLocal(criteria);
    }
}
